package com.jie.jie.mvp.util;

import android.util.Log;

import com.jie.jie.mvp.model.MeiziBean;

import java.util.ArrayList;

/**
 * Created by jie on 2017/1/23.
 */

public class DataUtil {
    public static ArrayList<MeiziBean> getMMHomeImgUrl(String json){
        ArrayList<MeiziBean> beans = new ArrayList<>();
        Log.d("getMMHomeImgUrl","start");
        try{
            json = json.substring(json.indexOf("<ul>"),json.indexOf("</ul>"));
            String[] imgs = json.split("<li");
            for(String img : imgs){
//                Log.d("getMMHomeImgUrl",img);
                if(!img.contains("_blank"))
                    continue;
                MeiziBean bean = new MeiziBean();
                img = img.substring(img.indexOf("href=\"")+6);
                bean.page = bean.from = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("src=\"")+5);
                bean.url = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("alt=\"")+5);
                bean.title = img.substring(0,img.indexOf("\""));
                Log.d("getMMHomeImgUrl",bean.toString());
                beans.add(bean);
            }
        }catch (Exception e){
            Log.d("getMMHomeImgUrl",e.getMessage());
        }
        return beans;
    }

    public static ArrayList<MeiziBean> getMMLabelImgUrl(String json){
        ArrayList<MeiziBean> beans = new ArrayList<>();
        Log.d("getMMLabelImgUrl","start");
        try{
            String[] imgs = json.split("<li");
            for(String img : imgs){
//                Log.d("getMMLabelImgUrl",img);
                if(!img.contains("_blank"))
                    continue;
                MeiziBean bean = new MeiziBean();
                img = img.substring(img.indexOf("href=\"")+6);
                bean.page = bean.from = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("src=\"")+5);
                bean.url = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("alt=\"")+5);
                bean.title = img.substring(0,img.indexOf("\""))+"\n";
                img = img.substring(img.indexOf("<i>")+3);
                bean.title += img.substring(0,img.indexOf("</i>"));
                Log.d("getMMLabelImgUrl",bean.toString());
                beans.add(bean);
            }
        }catch (Exception e){
            Log.d("getMMLabelImgUrl",e.getMessage());
        }
        return beans;
    }
}
